
;
(function($, $$) {
    $.fn.dropDownList = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.DropDownList);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.DropDownList = function(self, options) {
        if (!(this instanceof $$.DropDownList)) {
            return new $$.DropDownList(self, options);
        }
        var defaults = {

            dropDownListClass : $$.defaultGradientBG + " uiCornerAll " + $$.defaultBorder,

            selectListClass : $$.defaultGradientBG + " uiCornerAll uiBlackBorder uiBlackBoxShadow",

            pageSize : 5,

            showAll : false,

            multiple : false,

            dropListType : "normal",

            searchTip : "查找/search",

            hasScrollBar : false,

            listItemHoverClass : $$.hoverGradientBG,

            divideChar : ";",

            click : null,

            change : null,

            input : null,

            blur : null,

            focus : null,


            selectItemTotal : 0,

            listItemOuterHeight : 0,

            dropDownHTML : '<div class="uiDropDown"><div class="uiDropDownBtn uiSmallIcon uiDropdownButtonIcon uiBlackBorder"></div></div>',

            selectListHTML : '<div class="uiSelectList"></div>',

            noListDataHTML : '<li style="font-size:12px;">无数据项..</li>'
        };
        var opts = $.extend(defaults, options);
        $$.DropDownList.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.DropDownList, $$.Widget, {

        init : function() {
            var dropDownListObj = this;
            dropDownListObj.createWidget();
            initDropDownList(dropDownListObj.jqObj, dropDownListObj.opts);
            return dropDownListObj;
        },

        selectItem : function(index) {
            var dropDownListObj = this, self = dropDownListObj.jqObj, opts = dropDownListObj.opts;
            if (index === undefined) {
                return $(">.uiSelectList>.selected", self);
            }
            $("> .uiSelectList > li:eq(" + index + ")", self).click();
            return dropDownListObj;
        },

        selectList : function(list) {
            var dropDownListObj = this, self = dropDownListObj.jqObj, opts = dropDownListObj.opts;
            var selectList = $(".uiSelectList", self);
            if (list === undefined) {
                return selectList.children();
            }
            selectList.empty();
            if (typeof list === "string") {
                selectList.html(list);
            }
            else
            if ($.isArray(list)) {
                if (list.length === 0) {
                    selectList.append(opts.noListDataHTML);
                }
                $.each(list, function(i, item) {
                    selectList.append("<li>" + item + "</li>");
                });
            }
            else {
                selectList.append(list);
            }
            var listItem = selectList.find("> li");
            opts.selectItemTotal = listItem.length;
            if (opts.multiple) {
                listItem.prepend('<span class="uiSmallIcon uiSelectedIcon uiSelected"></span>');
            }
            var listShowTotal = opts.pageSize;
            if (opts.showAll || opts.pageSize > opts.selectItemTotal) {
                listShowTotal = opts.selectItemTotal;
            }
            if (opts.listItemOuterHeight === 0) {
                opts.listItemOuterHeight = selectList.children().outerHeight(true);
            }
            selectList.height(opts.listItemOuterHeight * listShowTotal);
            $("> .uiSelectList > li", self).hover(function() {
                $(this).addClass(opts.listItemHoverClass);
            }, function() {
                $(this).removeClass(opts.listItemHoverClass);
            });
            return dropDownListObj;
        },

        showSelectList : function() {
            var dropDownListObj = this, self = dropDownListObj.jqObj, opts = dropDownListObj.opts;
            self.children(".uiSelectList").show();
            return dropDownListObj;
        },

        hideSelectList : function() {
            var dropDownListObj = this, self = dropDownListObj.jqObj, opts = dropDownListObj.opts;
            self.children(".uiSelectList").hide();
            return dropDownListObj;
        },

        val : function() {
            var dropDownListObj = this, self = dropDownListObj.jqObj, opts = dropDownListObj.opts, selectedValue = [];
            self.find(">.uiSelectList>.selected").each(function() {
                selectedValue.push($(this).text());
            });
            return selectedValue;


        }
    });

    function initDropDownList(self, opts) {

        var multiple = opts.multiple ? "uiMultiple" : "";

        var selectList = self.children().addClass(multiple + " uiSelectList " + opts.selectListClass);
        opts.selectItemTotal = $("> li", selectList).length;
        var dropDown = $(opts.dropDownHTML);
        if (opts.dropListType === "search") {
            dropDown.append('<input type="text" value="' + opts.searchTip + '" class="uiCornerAll" />');
        }
        else {
            dropDown.append("<span></span>");
        }
        if (opts.multiple) {
            selectList.find("> li").prepend('<span class="uiSmallIcon uiSelectedIcon uiSelected"></span>');
        }
        self.append(dropDown)
            .append(selectList)
            .addClass("uiDorpDownList uiWidget " + opts.dropDownListClass);
        if (opts.dropListType === "search") {
            dropDown.children("input").width(dropDown.width() - 2 * parseInt(dropDown.css("padding-left")) - dropDown.children(".uiDropDownBtn").outerWidth(true));
        }

        if (opts.showAll) {
            opts.pageSize = opts.selectItemTotal;
        }
        else {
            opts.listItemOuterHeight = selectList.find("> li").outerHeight();
            selectList.height(opts.listItemOuterHeight * opts.pageSize);
        }
        selectList.hide();

        initEvent(self, opts);
        if (opts.disabled) {
        }
    }


    function initEvent(self, opts) {
        var selectedContent = $("> .uiDropDown > span, > .uiDropDown > input", self);

        $("> .uiDropDown", self).bind("click.uiDorpDownList", function(e) {
            if (opts.disabled) {
                return false;
            }
            if (opts.click) {
                if (opts.click.call(self[0], $(this), e) === false) {
                    return false;
                }
            }
            $(this).siblings(".uiSelectList").slideToggle(opts.animateTime);
        })
            .next("input").bind({
                "focus.uiDorpDownList" : function(e) {
                    if (opts.disabled) {
                        return false;
                    }
                    var obj = $(this);
                    if ($.isFunction(opts.focus)) {
                        if (opts.focus.call(self[0], obj, e) === false) {
                            return false;
                        }
                    }
                    if (!obj.hasClass("selected")) {
                        if (obj.val() === opts.searchTip) {
                            obj.val("");
                        }
                    }
                    obj.addClass("selected");
                },
                "blur.uiDorpDownList" : function(e) {
                    if (opts.disabled) {
                        return;
                    }
                    var obj = $(this);
                    if ($.isFunction(opts.blur)) {
                        if (opts.blur.call(self[0], obj, e) === false) {
                            return false;
                        }
                    }
                    if (obj.val() === "") {
                        obj.val(opts.searchTip);
                    }
                    obj.removeClass("selected");
                },
                "input.uiDorpDownList" : function(e) {
                    if (opts.disabled) {
                        return false;
                    }
                    if ($.isFunction(opts.input)) {
                        opts.input.call(self[0], $(this), e);
                    }
                }
            });

        var selectList = $("> .uiSelectList", self);
        if (opts.hasScrollBar) {
            selectList.show().scrollBar().hide();
        }
        else {
            $("> .uiSelectList", self).bind("mousewheel.uiDorpDownList", function(e, delta) {
                if (opts.disabled) {
                    return false;
                }
                if ($("> li", this).length <= opts.pageSize) {
                    return;
                }
                if (delta < 0) {
                    var obj = $("> li:not(:hidden):first", this);
                    if (obj.nextAll("li").length >= opts.pageSize) {
                        obj.hide();
                    }
                }
                else {
                    $("> li:hidden:last", this).show();
                }
                return false;
            });
        }
        selectList.bind("click.uiDorpDownList", function(e) {
            if (opts.disabled) {
                return;
            }
            var target = $(e.target);
            if ($.prop !== undefined) {
                if (target.prop("tagName").toUpperCase() != "LI") {
                    return;
                }
            }
            else
            if (target.attr("tagName").toUpperCase() != "LI") {
                return;
            }
            var selectedValue = target.toggleClass("selected").text();
            if (opts.multiple) {
                selectedValue = "";
                $(this).children(".selected").each(function() {
                    selectedValue += ($(this).text() + opts.divideChar);
                });
                selectedValue = selectedValue.substring(0, selectedValue.length - 1);
            }
            else {
                $(this).slideUp();
            }
            if (opts.dropListType === "search") {
                selectedContent.val(selectedValue);
            }
            else {
                selectedContent.html(selectedValue);
            }
            if ($.isFunction(opts.change)) {
                if (opts.change.call(self[0], $(this), selectedValue, e) === false) {
                    return false;
                }
            }
        });
        $("> .uiSelectList > li", self).hover(function() {
            $(this).addClass(opts.listItemHoverClass);
        }, function() {
            $(this).removeClass(opts.listItemHoverClass);
        });
        return self;
    }

})(jQuery, BASE);